import { TaskBase } from '../TaskBase';
import { ObjectWithStore } from '../Tasks';

export class TaskTransferAll extends TaskBase {
    shouldRun(creep: Creep) {
        // 指定 rType 可以选择要跳过的资源类型
        return this.peekFirstResource(creep) !== undefined;
    }

    _run(creep: Creep) {
        const thisResource = this.peekFirstResource(creep);
        return this.defaultProcess(creep.transfer(<ObjectWithStore>this.target, thisResource));
    }

    peekFirstResource(creep: Creep) {
        for (const rType in creep.store) {
            if (this.taskData.rType && this.taskData.rType === rType) {
                continue;
            }
            const amount = creep.store[this.taskData.rType] ?? 0;
            if (amount > 0) {
                return rType as ResourceConstant;
            }
        }
        return undefined;
    }
}